package com.jiuyun.servlet;

import com.jiuyun.dao.StudentaDao;
import com.jiuyun.dao.impi.StudentaDaoimpl;
import com.jiuyun.entity.PerUser;
import com.jiuyun.entity.User;
import com.jiuyun.entity.Userrole;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/login/*")
public class UserServlet extends BaseServlet {
    StudentaDao dao = new StudentaDaoimpl();

    protected void denglu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("登录");
        String name = request.getParameter("user-name");
        String pass = request.getParameter("password");
        try {
            List<Userrole> list=dao.denglu(name,pass);
            if (list!=null){
                System.out.println(list);
                for (Userrole  U :list){
                    request.getSession().setAttribute("uid", U.getUid());
                    if (U.getUser_role_id()==1){
                        HttpSession session=request.getSession();
                        session.setAttribute("user",list);
                        response.sendRedirect("/Eventstar/homepage.jsp");
                    }else if (U.getUser_role_id()==2){
                        HttpSession session=request.getSession();
                        session.setAttribute("user",list);
                        response.sendRedirect("/Eventstar/homepage.jsp");
                    }else if (U.getUser_role_id()==3){
                        HttpSession session=request.getSession();
                        session.setAttribute("user",list);
                        response.sendRedirect("/Eventstar/homepage.jsp");
                    }
                }
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    //注册
    protected void insert(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("name");
        String pass = request.getParameter("pass");
        int id=Integer.parseInt(request.getParameter("student"));
        User user=new User(name,pass,id);
        int a=0;
        try {
            a=dao.zhuce(user);
            System.out.println(user);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        if (user!=null){
            response.sendRedirect("/Eventstar/login.jsp");
        }else {
            response.sendRedirect("/Eventstar/register.jsp");
        }


//        if (pass.equals(pass) && ok != 0) {
//            response.sendRedirect(request.getContextPath() + "/login.html");
//        } else {
//            JOptionPane.showMessageDialog(null, "注册失败");
//        }


        //}
    }
}
